import create from "zustand";
import { devtools } from "zustand/middleware";

import {
  ChartType,
  // ChartType,
  WorkSpace,
} from "../models";

export interface WorkSpaceState {
  flights: string[];
  workSpace: WorkSpace;
}

export interface WorkSpaceAction {
  update: (workSpace: WorkSpace, flights?: string[]) => void;
}

const initState = (): WorkSpaceState => {
  return {
    flights: [],
    workSpace: {
      name: "",
      splitType: "disassemble",
      setting: {
        profileChartParmas: "",
        chartsHeight: "100",
        setBgByStateQuantity: false,
        stateQuantityParams: "",
        backgroundColors: [],
      },
      splitCharts: [],
    },
  };
};

export const useWorkSpaceStore = create<WorkSpaceState & WorkSpaceAction>(
  // @ts-ignore
  devtools((set, get, api) => ({
    ...initState(),
    update: (workSpace: WorkSpace, flights?: string[]) => {
      set({ workSpace, flights: flights || get().flights });
    },
  }))
);

export const useWorkSpace = () => {
  const { workSpace, update, flights } = useWorkSpaceStore();
  return {
    workSpace,
    update,
    flights,
  };
};
